package uark.csce.angli.wifidirectdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

/**
 * Created by ANG LI on 6/4/2015.
 */
public class ServerSocket extends AsyncTask <String,Void,Void>
{
    private Activity mContext;
    java.net.ServerSocket serverSocket;

    public ServerSocket(Activity context){
        mContext = context;
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            serverSocket=new java.net.ServerSocket(8666);

            Socket messageServer=serverSocket.accept();

            PrintStream out=new PrintStream(messageServer.getOutputStream());

            final String message = params[0];
            out.print(message);
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext, "Message Sent: " + message, Toast.LENGTH_SHORT).show();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
